import 'package:acgn_pioneer/widgets/reader/barrage/slide/barrage_controller.dart';
import 'package:acgn_pioneer/widgets/reader/copyright.dart';
import 'package:acgn_pioneer/widgets/reader/events/_index.dart';
import 'package:acgn_pioneer/widgets/reader/models/_reader_state.dart';
import 'package:acgn_pioneer/widgets/reader/reader_v.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_barrage.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_chapter.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_comic.dart';
import 'package:acgn_pioneer/widgets/reader/services/sr_image.dart';
import 'package:acgn_pioneer/widgets/reader/bottom_menu.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import '../../widgets/reader/_index.dart';

import '../../public.dart';

/// 阅读页组件
class ReaderPage extends StatefulWidget {
  /// 漫画id
  final int comicId;

  /// 是否嵌套在弹窗中
  final bool isPopNested;

  /// 章节id
  final int chapterId;

  ReaderPage({Key key, this.comicId, this.chapterId, this.isPopNested = false})
      : super(key: key);

  @override
  ReaderPageState createState() => ReaderPageState();

  static ReaderPageState of(BuildContext context) {
    assert(context != null);
    final ReaderPageState result =
        context.findAncestorStateOfType<ReaderPageState>();
    return result;
  }
}

class ReaderPageState extends State<ReaderPage>
    with AutomaticKeepAliveClientMixin {
  /// 章节加载service
  ReaderChapterService chapterService;

  /// 章节弹幕加载service
  ReaderBarrageService barrageService;

  /// 漫画service
  ReaderComicService comicService;

  // 图片加载
  ReaderImageLoaderService imageLoaderService;

  /// 阅读器数据仓库
  ReaderState state = ReaderState();

  /// 事件交互
  EventBus bus = EventBus();

  // 弹幕控制器
  BarrageController barrageController = BarrageController(BarrageSetting());

  ReaderPageState() {
    chapterService = ReaderChapterService();
    comicService = ReaderComicService(state: state);
    imageLoaderService = ReaderImageLoaderService(state, bus);
    barrageService = ReaderBarrageService();
  }

  var sub;
  @override
  void initState() {
    super.initState();
    initScreen();
    state.comicId = widget.comicId;
    state.chapterId = widget.chapterId;
    state.isPopNested = widget.isPopNested;

    sub = bus.on<ReaderRunEvent>().listen((event) {
      Future.delayed(Duration(microseconds: 500), () {
        setState(() {});
      });
    });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    sub.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
        width: state.screenWidth,
        height: state.screenHeight,
        color: Color(0xff1B1B26),
        child: LoadingComponent(
            maintainState: true,
            show: !state.isLoadCompleted,
            child: Stack(children: [
              state.isVerticalMode ? ReaderV() : ReaderH(),
              widget.isPopNested ? Container() : HeaderComponent(),
              ReaderBottomMenu(),
              ReaderRightMenu(),
              ReaderCopyRight(),
              ReaderUserBenefits(),
            ])));
  }

  initScreen() {
    state.screenWidth = ScreenUtil.getInstance().screenWidth;
    state.screenHeight = ScreenUtil.getInstance().screenHeight;
  }
}
